Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forward declaration of typedefed anonymous struct?

89 views
Skip to first unread message

Scott Schurr

unread,
Jan 11, 2001, 1:36:24 PM1/11/01
to
My question relates to using C header files in C++.

A common C idiom is to declare a struct in this form:

typedef struct {
int elementA;
int elementB;
} MyStruct;

This idiom allows the C programmer to just type "MyStruct"
in their C program instead of typing "struct MyStruct". It
gives MyStruct a few of the behaviors of a C++ class or
struct. The idiom is funny, though, because you end up
with a typedef of an anonymous struct. The struct has no
name; only the typedef has a name.

I'm writing a C++ program that uses some structs defined in
a C header with this C idiom of definition. I'd prefer not to
mess with the C header file if I can avoid it.

My problem is that I want to forward declare some of these
structs so I don't have to #include the C header file in my
C++ header file. By doing the forward declaration in my
C++ header file I would only need to include the C file in
my implementation. I want to keep the C-style structs from
spreading into the rest of my system.

When I try a forward declaration like this:

struct MyStruct;

I get an appropriate error message from my compiler:

conflicting types for `typedef struct MyStruct MyStruct'
previous declaration as `struct MyStruct'

I can't figure out any way to get the forward declaration
to work without changing the C header file to give the
struct a real name.

Advice?

--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Ron Natalie

unread,
Jan 11, 2001, 4:09:22 PM1/11/01
to

Scott Schurr wrote:
>
> My question relates to using C header files in C++.
>
> A common C idiom is to declare a struct in this form:
>
> typedef struct {
> int elementA;
> int elementB;
> } MyStruct;
>
>

> When I try a forward declaration like this:
>
> struct MyStruct;
>
> I get an appropriate error message from my compiler:
>
> conflicting types for `typedef struct MyStruct MyStruct'
> previous declaration as `struct MyStruct'
>
> I can't figure out any way to get the forward declaration
> to work without changing the C header file to give the
> struct a real name.
>

You can't do it that way, even in C.

typedef struct struct_tag {


int elementA;
int elementB;
} MyStruct;

could be forward declared (either language) as:

typedef struct struct_tag MyStruct;

but I'm not sure how to handle it for a struct with no type
name. However, your compiler hints that maybe it gave it
the name of the typedef (this isn't standard):
typedef struct MyStruct MyStruct;
might work as a forward delcartion.

Steve Clamage

unread,
Jan 12, 2001, 2:37:41 PM1/12/01
to
Ron Natalie wrote:

>
> Scott Schurr wrote:
> >
> > A common C idiom is to declare a struct in this form:
> >
> > typedef struct {
> > int elementA;
> > int elementB;
> > } MyStruct;
> >
> >
> > When I try a forward declaration like this:
> >
> > struct MyStruct;
> >
> > I get an appropriate error message from my compiler ...

> >
> > I can't figure out any way to get the forward declaration
> > to work without changing the C header file to give the
> > struct a real name.
> >
>
> You can't do it that way, even in C.
>
> typedef struct struct_tag {
> int elementA;
> int elementB;
> } MyStruct;
>
> could be forward declared (either language) as:
>
> typedef struct struct_tag MyStruct;

Or you can use the same name for the struct and the typedef:

typedef struct MyStruct { ... } MyStruct;

Now you can use "struct MyStruct" as well as "MyStruct" in both C and C++,
and forward-declare it as
struct MyStruct;
in either language.

But you cannot forward-declare a struct unless it has a tag.

--
Steve Clamage, stephen...@sun.com

scott_...@my-deja.com

unread,
Jan 12, 2001, 9:36:40 PM1/12/01
to
In article <3A5E5AC9...@sun.com>,

Thanks for the help, gentlemen. Since I'm trying to avoid
messing with the contents of the C header, I came up with
the following solution.

The C header file contains

typedef struct {
int elementA;
int elementB;
} MyStruct;

I can't forward declare MyStruct. So instead I forward declare

struct MyStruct_wrapper;

Then later, in my C++ implementation, I can define MyStruct_wrapper
as

struct MyStruct_wrapper {
MyStruct m;
};

While this wouldn't work for every circumstance, it works for
mine today because my goal is to keep the definition of MyStruct
out of my header.

I'm not sure why I didn't think of this solution sooner.

Thanks again for the help.

--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------


Sent via Deja.com
http://www.deja.com/

Ivan Vecerina

unread,
Jan 13, 2001, 2:55:28 PM1/13/01
to
scott_...@my-deja.com said:
>The C header file contains
>
> typedef struct {
> int elementA;
> int elementB;
> } MyStruct;
>
>I can't forward declare MyStruct. So instead I forward declare
>
> struct MyStruct_wrapper;
>
>Then later, in my C++ implementation, I can define MyStruct_wrapper
>as
>
> struct MyStruct_wrapper {
> MyStruct m;
> };

Nice solution. But why not:

struct MyStruct_wrapper : public MyStruct {};

No need to add m. everywhere in your code I think...

--
Ivan Vecerina - Surgical Navigation Network
--
Brainbench MVP for C++
http://www.brainbench.com

scott_...@my-deja.com

unread,
Jan 15, 2001, 11:15:31 AM1/15/01
to
In article <93p4ft$qeu$1...@news1.urbanet.ch>,

iv...@surgnav.com (Ivan Vecerina) wrote:
> scott_...@my-deja.com said:
> >The C header file contains
> >
> > typedef struct {
> > int elementA;
> > int elementB;
> > } MyStruct;
> >
> >I can't forward declare MyStruct. So instead I forward declare
> >
> > struct MyStruct_wrapper;
> >
> >Then later, in my C++ implementation, I can define MyStruct_wrapper
> >as
> >
> > struct MyStruct_wrapper {
> > MyStruct m;
> > };
>
> Nice solution. But why not:
>
> struct MyStruct_wrapper : public MyStruct {};
>
> No need to add m. everywhere in your code I think...

Cool. And it passes the "isa" test. Thanks very much. An
excellent solution.

--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------


Sent via Deja.com
http://www.deja.com/

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

0 new messages